package com.captain.leetcode2.链表;

import com.captain.leetcode.链表.ListNode;

/**
 * 存在一个按升序排列的链表，给你这个链表的头节点 head ，请你删除所有重复的元素，使每个元素 只出现一次 。
 * <p>
 * 返回同样按升序排列的结果链表。
 * 输入：head = [1,1,2]
 * 输出：[1,2]
 * <p>
 * 输入：head = [1,1,2,3,3]
 * 输出：[1,2,3]
 */
public class 删除排序链表中的重复元素84 {
    public static void main(String[] args) {
        new 删除排序链表中的重复元素84().deleteDuplicates(ListNode.getInstance());
    }

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) return null;
        //1 2 2 3 3
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode cur = dummy.next;
        ListNode curVal = new ListNode(-101);
        while (cur.next != null) {
            if (cur.val == curVal.val) {
                curVal.next = curVal.next.next;
                cur = cur.next;
                continue;
            }
            curVal = cur;
            cur = cur.next;

        }
        if (curVal.val == cur.val) curVal.next = null;
        return dummy.next;
    }
}
